package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.FormManagerAdapter;
import com.wnhz.work.yz.bean.FormListBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FormManagerActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView tv_right;
    private SwipeMenuListView my_listView;
    private FormManagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_manager);
        initView();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("发票管理");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("新增");
        tv_right.setTextColor(0xff008c99);

        my_listView = (SwipeMenuListView) findViewById(R.id.my_listView);
    }

    // TODO 吃屎吧吃屎吧
    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private void updelete(int position) {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("id", info1.get(position).getId());
        XUtil.Post(Url.MEMBER_DELINVOICE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");

                    if ("1".equals(res)) {
                        MyToast(info);
                        updown();
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private List<FormListBean.InfoEntity> info1 = new ArrayList<>();

    private void updown() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
        XUtil.Post(Url.MEMBER_GETINVOICELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    info1.clear();
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        FormListBean bean = gson.fromJson(result, FormListBean.class);
                        info1 = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setDate();
                super.onFinished();
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private void setDate() {
        mAdapter = new FormManagerAdapter(this, info1);
        my_listView.setAdapter(mAdapter);
        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {

                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(FormManagerActivity.this);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set item title
                deleteItem.setTitle("删除");
                // set item title fontsize
                deleteItem.setTitleSize(18);
                // set item title font color
                deleteItem.setTitleColor(Color.WHITE);
//                // set a icon
//                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        my_listView.setMenuCreator(creater);
        my_listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {

                    case 0:
                        //删除的逻辑
                        showDialogv7("确定删除该发票吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                updelete(position);
                                mAdapter.notifyDataSetChanged();
                            }
                        });
                        break;

                    default:
                        break;
                }
                return false;
            }
        });
        my_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           /*     Intent intent = new Intent(FormManagerActivity.this, AddAddressActivity.class);
                String province = addrList.get(position).getProvince();
                String city = addrList.get(position).getCity();
                String district = addrList.get(position).getDistrict();
                String place = addrList.get(position).getPlace();
                intent.putExtra("title", "修改地址");
                intent.putExtra("name", addrList.get(position).getConsignee());
                intent.putExtra("phone", addrList.get(position).getTelephone());
                intent.putExtra("province", province);
                intent.putExtra("city", city);
                intent.putExtra("district", district);
                intent.putExtra("place", place);
                intent.putExtra("addressId", addrList.get(position).getAddress_id());
                if (position == 0) {
                    intent.putExtra("def", "1");
                }
                startActivity(intent);*/
                if ("普通发票".equals(info1.get(position).getType_name())) {
                    startActivity(new Intent(FormManagerActivity.this, AddFormActivity.class).putExtra("id", info1.get(position).getId()).putExtra("isUpdate", 1));
                } else {
                    startActivity(new Intent(FormManagerActivity.this, TaxInvoiceActivity.class).putExtra("id", info1.get(position).getId()).putExtra("isUpdate", 1));
                }
            }
        });

        my_listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final int s = position;
                showDialogv7("确定删除发票吗？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        updelete(s);
                    }
                });

                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        Integer index;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //新增
            case R.id.submit:
                intent = new Intent(this, AddFormActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }


    @Override
    protected void onResume() {
        updown();
        super.onResume();
    }
}
